/*
* S.java
* Copyright (C) 2003
* 
* $Id: S.java,v 1.13 2005/12/13 00:00:25 salomo Exp $
*/
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
using System;
using Defines = Suake2.UI.Defines;
using cvar_t = Suake2.UI.game.cvar_t;
using Com = Suake2.UI.qcommon.Com;
using Cvar = Suake2.UI.qcommon.Cvar;
//UPGRADE_TODO: The type 'java.nio.ByteBuffer' could not be found. If it was not included in the conversion, there may be compiler issues. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1262'"
using ByteBuffer = java.nio.ByteBuffer;
namespace Suake2.UI.sound
{
	
	/// <summary> S</summary>
	public class S
	{
		public static System.String DriverName
		{
			get
			{
				return impl.Name;
			}
			
		}
		/// <summary> Returns a string array containing all sound driver names.</summary>
		public static System.String[] DriverNames
		{
			get
			{
				System.String[] names = new System.String[drivers.Count];
				for (int i = 0; i < names.Length; i++)
				{
					names[i] = ((Sound) drivers[i]).Name;
				}
				return names;
			}
			
		}
		/// <summary> This is used, when resampling to this default sampling rate is activated 
		/// in the wavloader. It is placed here that sound implementors can override 
		/// this one day.
		/// </summary>
		public static int DefaultSampleRate
		{
			get
			{
				return 44100;
			}
			
		}
		
		internal static Sound impl;
		internal static cvar_t s_impl;
		
		internal static System.Collections.ArrayList drivers = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(3));
		
		
		/// <summary> Registers a new Sound Implementor.</summary>
		public static void  register(Sound driver)
		{
			if (driver == null)
			{
				throw new System.ArgumentException("Sound implementation can't be null");
			}
			if (!drivers.Contains(driver))
			{
				drivers.Add(driver);
			}
		}
		
		/// <summary> Switches to the specific sound driver.</summary>
		public static void  useDriver(System.String driverName)
		{
			Sound driver = null;
			int count = drivers.Count;
			for (int i = 0; i < count; i++)
			{
				driver = (Sound) drivers[i];
				if (driver.Name.Equals(driverName))
				{
					impl = driver;
					return ;
				}
			}
			// if driver not found use dummy
			impl = (Sound) drivers[drivers.Count - 1];
		}
		
		/// <summary> Initializes the sound module.</summary>
		public static void  Init()
		{
			
			Com.Printf("\n------- sound initialization -------\n");
			
			cvar_t cv = Cvar.Get("s_initsound", "1", 0);
			if (cv.value_Renamed == 0.0f)
			{
				Com.Printf("not initializing.\n");
				useDriver("dummy");
				return ;
			}
			
			// set the last registered driver as default
			System.String defaultDriver = "dummy";
			if (drivers.Count > 1)
			{
				defaultDriver = ((Sound) drivers[drivers.Count - 1]).Name;
			}
			
			s_impl = Cvar.Get("s_impl", defaultDriver, Defines.CVAR_ARCHIVE);
			useDriver(s_impl.string_Renamed);
			
			if (impl.Init())
			{
				// driver ok
				Cvar.Set("s_impl", impl.Name);
			}
			else
			{
				// fallback
				useDriver("dummy");
			}
			
			Com.Printf("\n------- use sound driver \"" + impl.Name + "\" -------\n");
			StopAllSounds();
		}
		
		public static void  Shutdown()
		{
			impl.Shutdown();
		}
		
		/// <summary> Called before the sounds are to be loaded and registered.</summary>
		public static void  BeginRegistration()
		{
			impl.BeginRegistration();
		}
		
		/// <summary> Registers and loads a sound.</summary>
		public static sfx_t RegisterSound(System.String sample)
		{
			return impl.RegisterSound(sample);
		}
		
		/// <summary> Called after all sounds are registered and loaded.</summary>
		public static void  EndRegistration()
		{
			impl.EndRegistration();
		}
		
		/// <summary> Starts a local sound.</summary>
		public static void  StartLocalSound(System.String sound)
		{
			impl.StartLocalSound(sound);
		}
		
		/// <summary> StartSound - Validates the parms and ques the sound up
		/// if pos is NULL, the sound will be dynamically sourced from the entity
		/// Entchannel 0 will never override a playing sound
		/// </summary>
		public static void  StartSound(float[] origin, int entnum, int entchannel, sfx_t sfx, float fvol, float attenuation, float timeofs)
		{
			impl.StartSound(origin, entnum, entchannel, sfx, fvol, attenuation, timeofs);
		}
		
		/// <summary> Updates the sound renderer according to the changes in the environment,
		/// called once each time through the main loop.
		/// </summary>
		public static void  Update(float[] origin, float[] forward, float[] right, float[] up)
		{
			impl.Update(origin, forward, right, up);
		}
		
		/// <summary> Cinematic streaming and voice over network.</summary>
		public static void  RawSamples(int samples, int rate, int width, int channels, ByteBuffer data)
		{
			impl.RawSamples(samples, rate, width, channels, data);
		}
		
		/// <summary> Switches off the sound streaming.</summary>
		public static void  disableStreaming()
		{
			impl.disableStreaming();
		}
		
		/// <summary> Stops all sounds. </summary>
		public static void  StopAllSounds()
		{
			impl.StopAllSounds();
		}
		/// <summary> Searches for and initializes all known sound drivers.</summary>
		static S()
		{
			{
				// dummy driver (no sound)
				try
				{
					//UPGRADE_TODO: The differences in the format  of parameters for method 'java.lang.Class.forName'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
					System.Type.GetType("jake2.sound.DummyDriver");
					// initialize impl with the default value
					// this is  necessary for dedicated mode
					useDriver("dummy");
				}
				//UPGRADE_NOTE: Exception 'java.lang.Throwable' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
				catch (System.Exception e)
				{
					Com.DPrintf("could not init dummy sound driver class.");
				}
				
				try
				{
					//UPGRADE_TODO: The differences in the format  of parameters for method 'java.lang.Class.forName'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
					System.Type.GetType("org.lwjgl.openal.AL");
					//UPGRADE_TODO: The differences in the format  of parameters for method 'java.lang.Class.forName'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
					System.Type.GetType("jake2.sound.lwjgl.LWJGLSoundImpl");
				}
				//UPGRADE_NOTE: Exception 'java.lang.Throwable' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
				catch (System.Exception e)
				{
					// ignore the lwjgl driver if runtime not in classpath
					Com.DPrintf("could not init lwjgl sound driver class.");
				}
				
				// prefered driver
				try
				{
					//UPGRADE_TODO: The differences in the format  of parameters for method 'java.lang.Class.forName'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
					System.Type.GetType("net.java.games.joal.AL");
					//UPGRADE_TODO: The differences in the format  of parameters for method 'java.lang.Class.forName'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
					System.Type.GetType("jake2.sound.joal.JOALSoundImpl");
				}
				//UPGRADE_NOTE: Exception 'java.lang.Throwable' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
				catch (System.Exception e)
				{
					// ignore the joal driver if runtime not in classpath
					Com.DPrintf("could not init joal sound driver class.");
				}
			}
		}
	}
}